package com.shangqing.web.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.shangqing.common.core.domain.AjaxResult;
import com.shangqing.system.entity.CityGrade;
import com.shangqing.system.entity.Earthquake;
import com.shangqing.system.entity.EerthNumber;
import com.shangqing.system.service.ISysEarthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Api(value = "pet", description = "地震信息接口")
@RestController
public class SysEarthQuakeController {
	@Autowired
	private ISysEarthService iSysEarthService;
	@Autowired
	private RestTemplate restTemplate;
	/**
	 * 获取地震参数
	 *
	 * @return
	 */
	@ApiOperation(value = "查找所有地震信息")
	@ResponseBody
	@GetMapping("/earthinfo")
	public AjaxResult showinfo() {
		List<Earthquake> earthquakes = iSysEarthService.selectearthinfo();
		if (earthquakes.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", earthquakes);
		}
	}

	@ApiOperation(value = "查询每个城市地震所占百分比")
	@ResponseBody
	@GetMapping("/shownum")
	public AjaxResult shownum() {
		List<EerthNumber> eerthNumbers = iSysEarthService.selectearthnum();
		if (eerthNumbers.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", eerthNumbers);
		}
	}

	@ApiOperation(value = "统计每个城市不同震别的次数")
	@ResponseBody
	@PostMapping("/countgrade")
	public AjaxResult countgrade(@RequestParam("year") int year) {
		List<CityGrade> cityGrades = iSysEarthService.selectcountgrade(year);
		if (cityGrades.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", cityGrades);
		}
	}

	@ApiOperation(value = "查询人口损失")
	@ResponseBody
	@PostMapping("/populationloss")
	public AjaxResult populationloss(@RequestParam("year") int year) {
		List<Map> maps = iSysEarthService.selectpopulation(year);
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}

	@ApiOperation(value = "查询房屋损失")
	@ResponseBody
	@PostMapping("/homeloss")
	public AjaxResult homeloss(@RequestParam("year") int year) {
		List<Map> maps = iSysEarthService.selecthomeloss(year);
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}

	@ApiOperation(value = "查询经济损失")
	@ResponseBody
	@PostMapping("/moneyloss")
	public AjaxResult moneyloss(@RequestParam("year") int year) {
		List<Map> maps = iSysEarthService.selectmoneyloss(year);
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}

	@ApiOperation(value = "年度各震级发生次数")
	@ResponseBody
	@PostMapping("/counteveryyear")
	public AjaxResult counteveryyear(@RequestParam("year") int year) {
		List<Map> maps = iSysEarthService.selecteveryyear(year);
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}

	@ApiOperation(value = "查询近五年每年发生的地震次数")
	@ResponseBody
	@GetMapping("/countfiveyear")
	public AjaxResult countfiveyear() {
		List<Map> maps = iSysEarthService.selectfiveyear();
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}

	@ApiOperation(value = "根据id查询死亡人数和房屋倒塌数")
	@ResponseBody
	@PostMapping("/selectdiedhome")
	public AjaxResult selectdiedhome(@RequestParam("id") int id) {
		List<Map> maps = iSysEarthService.selectdiehome(id);
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}

	@ApiOperation(value = "全区灾情统计五个月")
	@ResponseBody
	@GetMapping("/selectfivem")
	public AjaxResult selectfivem() {
		List<Map> maps = iSysEarthService.selectfivemonth();
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}

	@ApiOperation(value = "震源50km影响")
	@ResponseBody
	@PostMapping("/selectInfluence")
	public AjaxResult selectInfluence(@RequestParam("id") int id) {
		List<Map> maps = iSysEarthService.selectinfluence(id);
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}

	@PostMapping("/forecast/{cityid}")
	@ApiOperation(value = "天气预报", notes = "", httpMethod = "POST")
	public AjaxResult forecastyinchuan(int cityid) {
		String weatherUrl =  "https://restapi.amap.com/v3/weather/weatherInfo?city="+cityid+"&key=ed8913582dbbc7b27152c793cfac49b7&extensions=all";
		ResponseEntity<String> weatherEntity = restTemplate.getForEntity(weatherUrl, String.class);
		String weatherBody = weatherEntity.getBody();
		JSONObject weatherObject = JSONObject.parseObject(weatherBody);
		System.out.println(weatherObject);
		return AjaxResult.success("查询成功", weatherObject);
	}

	@PostMapping("/lives/{cityid}")
	@ApiOperation(value = "实时天气", notes = "", httpMethod = "POST")
	public AjaxResult livesyinchuan(int cityid) {
		String weatherUrl =  "https://restapi.amap.com/v3/weather/weatherInfo?city="+cityid+"&key=ed8913582dbbc7b27152c793cfac49b7&extensions=base";
		ResponseEntity<String> weatherEntity = restTemplate.getForEntity(weatherUrl, String.class);
		String weatherBody = weatherEntity.getBody();
		JSONObject weatherObject = JSONObject.parseObject(weatherBody);
		System.out.println(weatherObject);
		return AjaxResult.success("查询成功", weatherObject);
	}

	@ApiOperation(value = "最近一周的地震（4-8）级")
	@ResponseBody
	@PostMapping("/selectfiveday")
	public AjaxResult selectfiveday() {
		List<Map> maps = iSysEarthService.selectfivedays();
		if (maps.isEmpty()) {
			return AjaxResult.error("没有找到");
		} else {
			return AjaxResult.success("找到了", maps);
		}
	}
}
